/**
 * @author 鍚存倍
 * @since 2022/10/13
 * 13:47
 */
package com.windgo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windgo.bean.MarketSearchHistory;
import com.windgo.bean.MarketSearchHistoryExample;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.CommonData;
import com.windgo.mapper.MarketSearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class HistoryServiceImpl implements HistoryService{
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Override
    public CommonData<MarketSearchHistory> list(BasePageInfo basePageInfo, Integer id) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.setOrderByClause(basePageInfo.getSort()+" "+basePageInfo.getOrder());
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
        if(id!=null){
            criteria.andIdEqualTo(id);
        }
        List<MarketSearchHistory> list = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        PageInfo<MarketSearchHistory> marketSearchHistoryPageInfo = new PageInfo<>(list);
        return CommonData.data(marketSearchHistoryPageInfo);
    }

    @Override
    public CommonData<MarketSearchHistory> search(BasePageInfo basePageInfo, Integer userId, String keyword) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.setOrderByClause(basePageInfo.getSort()+" "+basePageInfo.getOrder());
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(keyword!=null&&!" ".equals(keyword)){
            criteria.andKeywordLike("%"+keyword+"%");
        }
        List<MarketSearchHistory> list = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        PageInfo<MarketSearchHistory> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }
}

